package com.ross.service.impl;

import com.ross.common.PasswordHelper;
import com.ross.dao.SysUsersMapper;
import com.ross.pojo.SysPermissions;
import com.ross.pojo.SysRoles;
import com.ross.pojo.SysUsers;
import com.ross.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author RossZhang
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUsersMapper userDao;
    @Autowired
    private PasswordHelper passwordHelper;
    @Autowired
    public void setUserDao(SysUsersMapper userDao) {
        this.userDao = userDao;
    }
    @Autowired
    public void setPasswordHelper(PasswordHelper passwordHelper) {
        this.passwordHelper = passwordHelper;
    }

    /**
     * 判断当前的用户和角色是否存在
     *
     * @param userId
     * @param roleId
     * @return
     */
    public boolean exists(Long userId, Long roleId) {
        return this.userDao.exists(userId, roleId);
    }

    /**
     * 添加用户-角色关系
     * @param userId
     * @param roleIds
     */
    @Override
    public void correlationRoles(Long userId, Long... roleIds) {
        if (roleIds == null || roleIds.length == 0){
            return;
        }
        for (Long roleId:roleIds){
            if (!exists(userId,roleId)){
                this.userDao.correlationRoles(userId,roleId);
            }
        }
    }

    /**
     * 移除用户-角色关系
     * @param userId
     * @param roleIds
     */
    @Override
    public void uncorrelationRoles(Long userId, Long... roleIds) {
        if (roleIds == null || roleIds.length == 0) {
            return;
        }
        for (Long roleId: roleIds) {
            if (exists(userId, roleId)) {
                this.userDao.uncorrelationRoles(userId, roleId);
            }
        }
    }

    /**
     * 根据用户名查找其角色
     * @param username
     * @return
     */
    @Override
    public List<SysRoles> findRoles(String username) {
        return this.userDao.findRoles(username);
    }

    /**
     * 根据用户名查找其权限
     * @param username
     * @return
     */
    @Override
    public List<SysPermissions> findPermissions(String username) {
        return this.userDao.findPermissions(username);
    }

    /**
     *
     * @param userId
     * @param newPassword
     */
    @Override
    public void changePassword(Long userId, String newPassword) {
        SysUsers user = this.userDao.findById(userId);
        user.setPassword(newPassword);
        passwordHelper.encryptPassword(user);
        this.userDao.update(user);
    }

    @Override
    public SysUsers findByName(String username) {
        return this.userDao.findByName(username);
    }

    @Override
    public void deleteAllUserRoles(Long id) {
        this.userDao.deleteAllUserRoles(id);
    }

    @Override
    public SysUsers findById(Long id) {
        return this.userDao.findById(id);
    }

    @Override
    public void create(SysUsers sysUsers) {
        //加密密码
        this.passwordHelper.encryptPassword(sysUsers);
        this.userDao.create(sysUsers);
    }

    @Override
    public void delete(Long id) {
        this.userDao.delete(id);
    }

    @Override
    public void update(SysUsers sysUsers) {
        this.userDao.update(sysUsers);
    }

    @Override
    public List<SysUsers> findAll() {
        return this.userDao.findAll();
    }
}
